﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DSVISUAL
{
    public partial class ConsoleForm : Form
    {
        Process process;
        public StreamWriter Input;
        public StreamReader Output;
        public ConsoleForm()
        {
            InitializeComponent();
            this.FormClosing += ConsoleForm_FormClosing;
            this.textBox1.KeyPress += TextBox1_KeyPress;
            this.textBox1.TextChanged += TextBox1_TextChanged;
            this.Activated += ConsoleForm_Activated;
            process = new Process();
            process.StartInfo = new ProcessStartInfo()
            {
                LoadUserProfile = true,
                //StandardErrorEncoding = Encoding.UTF8,
                StandardOutputEncoding = Encoding.UTF8,
                UseShellExecute = false,
                WorkingDirectory = Application.StartupPath,
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                //RedirectStandardError = true,
                FileName = "cmd.exe",
                CreateNoWindow = true,
            };
            process.ErrorDataReceived += Process_ErrorDataReceived;
            process.OutputDataReceived += Process_OutputDataReceived;
            process.Exited += Process_Exited;
            process.Start();
            Input = process.StandardInput;
            Output = process.StandardOutput;
        }

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox1_TextChanged(null, null);
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            this.toolStripStatusLabel1.Text =
                string.Format("Line {0} ,Column {1}, SelectionStart {2}, SelectionLength {3}", 0, 0, textBox1.SelectionStart, textBox1.SelectionLength);
        }

        public int pos = 0;

        private void ConsoleForm_Activated(object sender, EventArgs e)
        {
            textBox1.Focus();
            textBox1.Select(0, pos);
        }

        private void TextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            TextBox1_TextChanged(null, null);
            if (e.Control && e.KeyCode == Keys.C)
            {
                this.Close();
            }
            else if (e.KeyCode == Keys.Return)
            {
                this.Input.WriteLine();
            }
        }

        private void ConsoleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (process != null)
            {
                process.CloseMainWindow();
                process.Close();
            }
        }

        private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            this.textBox1.AppendText("Error:" + e.Data);
        }

        private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            this.textBox1.AppendText(e.Data);
        }

        private void Process_Exited(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
